/*
 * main.cpp
 *Creational Pattern: SINGLETON
 *  Created on: Sep 14, 2011
 *      Author: gassa
 */

#include <string>
#include <iostream>
#include <fstream>
#include <map>
#include <utility>
#include <hash_map>
#include "../include/CUtilities.h"
#include "../include/config.h"

using namespace std;

struct CategoryIndexValue {
	int count;
	CategoryIndexValue(const int _count) : count(_count) {}
};

typedef map< size_t, map<size_t, CategoryIndexValue> > CATEGORYINDEXMAP;
//typedef map< size_t, string > INVERTEDINDEXMAP;

class CategoryIndex {
private:
	static bool instanceFlag;
	static CategoryIndex *builderInstance;
	int barrelNumber;
	int currentBarreal;
	config *configInstance;
	string dirPath;
	int barrelSize;
	map<char, int> barrelMapper;

	CATEGORYINDEXMAP* categoryMap;
	int* postingListCounter;
	ofstream* barrelStream;
	int* barrelCounter;


	void writePostingList(CATEGORYINDEXMAP::iterator iter, ofstream& stream);
	void writeIndexToFile();
	void loadBarrelFile(int barrelNumber, map<int, string>& container);
public:
	~CategoryIndex() {
		instanceFlag = false;
		delete configInstance;
	};
	static CategoryIndex* getInstance();
	CategoryIndex();
	void addCategoryPostingList(size_t tid, size_t fid, string term);
	void flushInstance();
	void setCurrentBarrel(string term);
	void closeAll();
	void printMAP();
	void mergeIndex();
	void setBarrelNumberToMap();
};
